﻿

$(document).ready(function() {
    /** Inline Validation */
    jQuery.fn.extend({
        /** field is required */
        require: function(failMsg) {
            var element = $(this);
            return $(this).queue('validators', function() {
                if (element.isDefaultValue()) {
                    element.data('error', failMsg);
                    return false;
                }
                return true;
            });
        },
        /** field has a minimum length */
        minimumLength: function(minLength, failMsg) {
            var element = $(this);
            return $(this).queue('validators', function() {
                if (element.val().length < minLength) {
                    element.data('error', failMsg);
                    return false;
                }
                return true;
            });
        },
        /** field has a maximum length */
        maximumLength: function(maxLength, failMsg) {
            var element = $(this);
            return $(this).queue('validators', function() {
                if (element.val().length > maxLength) {
                    element.data('error', failMsg);
                    return false;
                }
                return true;
            });
        },
        /**  matches a regular expression */
        matchRegex: function(regex, failMsg) {
            var element = $(this);
            return $(this).queue('validators', function() {
                if (!regex.test(element.val())) {
                    element.data('error', failMsg);
                    return false;
                }
                return true;
            });
        },
        /** matches value against that of another form element */
        matchElement: function(targetElement, failMsg) {
            var element = $(this);
            return $(this).queue('validators', function() {
                if (element.val() !== targetElement.val()) {
                    element.data('error', failMsg);
                    return false;
                }
                return true;
            });
        },
        /** validate element */
        isValid: function() {
            var validators = $(this).queue('validators');
            for (key in validators) {
                if (!validators[key]($(this))) {
                    return false;
                }
            }
            return true;
        },
        /** regex find and replace */
        filterRegex: function(regex, replacement) {
            var element = $(this);
            return $(this).queue('filters', function() {
                var data = element.val();
                element.val(data.replace(regex, replacement));
            });
        },
        /** run data filters on field */
        filterData: function() {
            var filters = $(this).queue('filters');
            for (key in filters) {
                filters[key]($(this));
            }

        },
        /** Validates select date group */
        checkDateGroup: function(failMsg) {
            var dateSelects = $(this);
            return dateSelects.each(function() {
                $(this).queue('validators', function(element) {
                    //check all selects have values
                    var valid = true;
                    dateSelects.each(function() {
                        var select = $(this);
                        if (select.isDefaultValue()) {
                            valid = false;
                        }
                    });
                    if (!valid) {
                        element.data('error', failMsg);
                        return false;
                    }
                    //check valid date
                    var date = new Date();
                    date.setYear(Number(dateSelects.filter("select[name$='Year']").val()));
                    date.setMonth(Number(dateSelects.filter("select[name$='Month']").val()) - 1);
                    date.setDate(Number(dateSelects.filter("select[name$='Day']").val()));

                    //date object day will not match select value on bad date ie Feb 31
                    if (date.getDate() != Number(dateSelects.filter("select[name$='Day']").val())) {
                        valid = false;
                        element.data('error', 'Ngày sinh không hợp lệ');

                    }
                    return valid;
                });
            });
        },
        /** monitor element and trigger validation when altered */
        watch: function() {
            $(this).bind('blur', function() {
                $(this).filterData();
            });
            $(this).bind('blur change', function() {
                if ($(this).isValid()) {
                    $(this).flyoutSuccess();
                } else {
                    $(this).flyoutFail($(this).data('error'));
                }
            });
            $(this).bind('focus', function() {
                $(this).flyoutInfo();
            });
        },
        check: function() {
            if ($(this).isValid()) {
                return true;
            } else {
                submit = false;
                $(this).flyoutFail($(this).data('error'));
                $(this).focus();
                return false;
            }

        },
        /** check if element is set to its default value */
        isDefaultValue: function() {
            var element = $(this);
            if ('SELECT' == element.attr('tagName')) {
                return (-1 == element.val());
            }
            if ('radio' == element.attr('type')) {
                return (0 == element.filter(':checked').length);
            }
            return ('' == element.val());
        },
        /** Configure flyouts for valid element */
        flyoutHide: function() {
        },
        flyoutSuccess: function() {
        },
        /** Configure flyouts for non-valid element */
        flyoutFail: function(msg) {
            alert(msg);
        },
        /** Configure flyouts to show info */
        flyoutInfo: function() {
            var flyouts = $(this).parents('td').next().children('.flyout');
            flyouts.filter('.bad').hide();
            flyouts.filter('.good').hide();
            flyouts.filter('.info').show();
        }

    });
    /** Validation Notifications */
    jQuery.fn.extend({
        /** display correct flyouts on load for form elements */
        displayValidationFlyouts: function() {
            var element = $(this);
            if (!element.isDefaultValue()) {
                element.flyoutHide();
            }
            else {

                if ('' !== element.getValidationErrorMessage()) {
                    element.flyoutFail();
                }
            }
        },
        /** display correct flyouts on load for radio form elements */
        displayValidationFlyoutsRadio: function() {
            var radios = $(this);
            if ('' !== $(radios[0]).parent().getValidationErrorMessage()) {
                $(radios[0]).parent().flyoutFail();
                return;
            }
            if (1 == radios.filter(':checked').length) {
                $(radios[0]).parent().flyoutSuccess();
            }
        },
        /** get error message from flyout */
        getValidationErrorMessage: function() {
            var errorNode = $(this).parents('td').next().children('.bad');
            if (errorNode.length == 0) {
                return '';
            }
            return errorNode.html().split(' ').join();
        }
    });
});


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//$(document).ready(function() {
//    setTabKeyPress();
//    setEnterKey('.enterKey');
////    setNumberOnly('.numberOnly');
////    setFormat('.dateOnly', '9999/99/99');
//});

//function setTabKeyPress() {
//    var obj = $("input");
//    for (var i = 0; i < obj.length; i++) {
//        obj[i].setAttribute("onkeydown", " if(event.keyCode == 9){return true;}");
//    }
//}

//function setEnterKey(name) {
//    var obj = $(name);
//    for (var i = 0; i < obj.length; i++) {
//        var curFunc = "";
//        if (obj[i].getAttribute("onkeydown"))
//            curFunc = obj[i].getAttribute("onkeydown");
//        if (i != obj.length - 1)
//            obj[i].setAttribute("onkeydown", curFunc + " if(event.keyCode == 13){$('#" + obj[i + 1].id + "').focus(); return false;}");
//        else {
//            var submitbtnlist = $(".submit");
//            var submitbtn = submitbtnlist[0];
//            obj[i].setAttribute("onkeydown", curFunc + " if(event.keyCode == 13){$('#" + submitbtn.id + "').focus(); return false;}");
//        }
//    }
//}

//function setNumberOnly(name) {
//    var obj = $(name);
//    for (var i = 0; i < obj.length; i++) {
//        var curFunc = "";
//        if (obj[i].getAttribute("onkeydown"))
//            curFunc = obj[i].getAttribute("onkeydown");
//        obj[i].setAttribute("onkeydown", curFunc + " if(event.keyCode < 48 || event.keyCode > 57) return false;");
//    }
//}
//function numbersonly(name) {
//    setNumberOnly(name);
//}

//function setFormat(name, fmt) {
//    var obj = $(name);
//    for (var i = 0; i < obj.length; i++) {
//        var curFunc = "";
//        if (obj[i].getAttribute("onkeydown"))
//            curFunc = obj[i].getAttribute("onkeydown");
//        obj[i].setAttribute("onkeydown", curFunc + " if(event.keyCode < 48 || event.keyCode > 57) return false;");
//        $("#" + obj[i].id).mask(fmt, { placeholder: "_" });
//    }
//}

///THANH///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function funcNextBox(nxtIdx, submitButton, enterCount,cssClass) {
    if (nxtIdx == enterCount) {
        if (document.getElementById(submitButton).disabled != true) {
            document.getElementById(submitButton).focus();
        }
    }
    else {
        if ($("." + cssClass + ":eq(" + parseInt(nxtIdx) + ")").attr('id') != "" && $("." + cssClass + ":eq(" + parseInt(nxtIdx) + ")").attr('id') != null) {
            $("." + cssClass + ":eq(" + parseInt(nxtIdx) + ")").focus();
        } else {
            $("." + cssClass + ":eq(" + parseInt(nxtIdx) + ")").children().focus();
        }
    }
}
var var_registerControlToValidation = false;

function registerControlToValidation(cssClass) {
    if (!var_registerControlToValidation) 
    {
        $("." + cssClass).each(function() {
        //document.getElementById($(this).attr('id')).setAttribute('onkeypress', 'return false;');
            if (document.getElementById($(this).attr('id')).getAttribute('onblur') == null) {
                document.getElementById($(this).attr('id')).setAttribute('onblur', "hidePopup(this);ValidatorOnChange(event);");
            } else {
                document.getElementById($(this).attr('id')).setAttribute('onblur', document.getElementById($(this).attr('id')).getAttribute('onblur') + ";hidePopup(this);ValidatorOnChange(event);");
            }
        })
    
        if ($.browser.mozilla) {

            $("." + cssClass).each(function() {
                var element = $(this);
                document.getElementById(element.attr('id')).setAttribute('onkeypress', "disableValidateFireFox(event);" + document.getElementById(element.attr('id')).getAttribute('onkeypress'));
            })
        }
        
        var_registerControlToValidation = true;
    }
}

function hidePopup(element) {
    if (AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout != null) {
        AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout.hide();
        AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout = null;
    }
    else {
        if (element != null) {
            $("#" + element.id).removeClass("invalid invalid1 invalid2 invalid3 invalid4 invalid5");
        }
    }
}

function disableValidateFireFox(e) {
    e = e || window.event;
    var code = e.keyCode || e.which;
    if (code == 13)
        return false;
}

function registerEnterEvent(cssClass, submitButton, enterCount) {
        $("form").keydown(function(e) {
            var key = e.which;
            if (key == 13 && !e.shiftKey) {
                return false;
            }
        })
        $("input[type=submit]").bind("keydown", function(e) {
        var key = e.which;
            if (key == 13 && !e.shiftKey) {
                $(this).click();
            }
        })
    var $inp = $('.' + cssClass);
    $inp.bind('keydown', function(e) {
        var key = e.which;
        if (key == 13 && !e.shiftKey) {
            e.preventDefault();
            var nxtIdx = $inp.index(this) + 1;
            setTimeout(function() {
                while (($("." + cssClass + ":eq(" + parseInt(nxtIdx) + ")") == null || $("." + cssClass + ":eq(" + parseInt(nxtIdx) + ")").attr("disabled") == "disabled" || $("." + cssClass + ":eq(" + parseInt(nxtIdx) + ")").attr("disabled") == true) && nxtIdx < enterCount) {
                    nxtIdx += 1;
                }
                funcNextBox(nxtIdx, submitButton, enterCount, cssClass);
            }, 0);
            return false;
            //setTimeout(function() { funcNextBox(nxtIdx, submitButton, enterCount, cssClass) }, 300);
        }
    });
//    $("." + cssClass).each(function() {
//        document.getElementById($(this).attr("id")).focus();
//        return false;
//    })
}

function focusToControlErr(cssClass) {
    setTimeout(function() {
        var css = (typeof (cssClass) === 'undefined' ? ".validation" : "." + cssClass);
        $(css).each(function() {
            var element = $(this);
            var cssClass = element.attr("class");
            var arrInvalid = Array("invalid", "invalid1", "invalid2", "invalid3", "invalid4", "invalid5");
            for (var i = 0; i < arrInvalid.length; i++) {
                if (cssClass.indexOf(arrInvalid[i]) != -1) {
                    if (element.attr("disabled") != "disabled" && element.attr("disabled") != "true") {
                        element.focus();
                        return false;
                    }
                }
            }
        });
    }, 200);
}

function NotInputSpecialKeys(myfield, e, dec) {
            
                var key;
                var keychar;

                if (window.event)
                    key = window.event.keyCode;
                else if (e)
                    key = e.which;
                else
                    return true;
                keychar = String.fromCharCode(key);

                if (keychar == ',')
                    return false;

                // control keys
                if ((key == null) || (key == 0) || (key == 8) ||
                (key == 9) || (key == 13) || (key == 27) || (key == 44))
                    return true;
                else if ((typeof (dec) == "undefined" ? "" : dec).indexOf(keychar) > -1) {
                    return true;
                } else if ((("`~!@#$%^&*()-_+=[]{};:'\"\\|.,<>/? ").indexOf(keychar) > -1))
                    return false;

                else
                    return true;

            }
function InputNumberKeys(myfield, e, dec) {

    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;
    keychar = String.fromCharCode(key);

    if (keychar == ',')
        return false;

    // control keys
    if ((key == null) || (key == 0) || (key == 8) ||
    (key == 9) || (key == 13) || (key == 27) || (key == 44))
        return true;
    else if ((("0123456789" + (typeof(dec) == "undefined" ? "" : dec)).indexOf(keychar) > -1))
        return true;

    else
        return false;

}
function InputJPKeys(myfield, e, dec) {

    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;
    keychar = String.fromCharCode(key);

    if (keychar == ',')
        return false;

    // control keys
    if ((key == null) || (key == 0) || (key == 8) ||
    (key == 9) || (key == 13) || (key == 27) || (key == 44))
        return true;
    else {
        return textAreaLengthCheck(keychar, 2);
    }
    
    function textAreaLengthCheck(val, maxLengthByte) {
        if (escape(val).length > 4) {
            return true;
        } else {
            return false;
            }
    }
}
function formatDate(cssClass, style) {
    function split(element) {
        var mydate = element.val();
        if (mydate.length == 8) {
            var year = mydate.substr(0, 4);
            var month = mydate.substr(4, 2);
            var day = mydate.substr(6, 2);
            element.val(year + style + month + style + day);
        }
        else if (mydate.length < 8) {
            
        }
    }


    $("." + cssClass).bind("keydown blur", function(e) {
        if (e.type == "keydown") {
            var key = e.which;
            if ((key == 9 || key == 13) && !e.shiftKey) {
                split($(this));
            }
        }
        else if (e.type = "blur") {
            split($(this));
        }
    })

    
    $("." + cssClass).bind("focus", function() {
        var element = $(this);
        var mydate = element.val();
        if (mydate.length == 10) {
            var value = mydate.split(style);
            var valNew = "";
            for (var i = 0; i < value.length; i++) {
                valNew = valNew + value[i];
            }
            element.val(valNew);
        }
    })
    $("." + cssClass).each(function() {
    var element = $(this);
    document.getElementById(element.attr("id")).setAttribute("autocomplete", "off");
        document.getElementById(element.attr("id")).setAttribute("maxlength", "8");
        document.getElementById(element.attr("id")).setAttribute("onkeypress", "return InputNumberKeys(this, event)");
        document.getElementById(element.attr("id")).setAttribute("onpaste", "return false;");
        element.addClass("disable_ime-mode");
    })
}

function formatMoney(cssClass, style1, style2, dec, align) {
    function ReplaceAll(Source, stringToFind, stringToReplace) {
        var temp = Source;
        var index = temp.indexOf(stringToFind);
        while (index != -1) {
            temp = temp.replace(stringToFind, stringToReplace);
            index = temp.indexOf(stringToFind);
        }
        return temp;
    }
    function mysplit(data, style1) {

        var val = ReplaceAll(data, style1, '');
        var val_arr = val.split('');
        var mirror_val = '';
        var return_val = '';
        var num = 0;
        for (i = (val_arr.length - 1); i >= 0; i--) {
            if (num == 3) {
                mirror_val += style1 + val_arr[i];
                num = 0;
            } else {
                mirror_val += val_arr[i];
            }
            num++;
        }

        var mirror_val_arr = mirror_val.split('');

        for (i = (mirror_val_arr.length - 1); i >= 0; i--) {
            return_val += mirror_val_arr[i];
        }
        return return_val;
    }
    function removeZero(val) {
        if (val != "") {
            while (val.length != 0) {
                if (val.charAt(0) == "0") {
                    val = val.substring(1, val.length);
                }
                else {
                    if (val.charAt(0) == style2) {
                        return "0" + val;
                    }
                    return val;
                }
            }
            if (val.length == 0)
                return "0";
            return val;
        }
        return "";
    }
    function split(element, style1, style2) {
        var chuoi;
        if (element.val().charAt(0) == "-") {
            chuoi = element.val().substring(1, element.val().length);
            chuoi = removeZero(chuoi);
        }
        else {
            chuoi = element.val();
            chuoi = removeZero(chuoi);
        }
        if (checknumber(chuoi, style1)) {
            if (chuoi == "") return;
            var index_dot_end = chuoi.lastIndexOf(style2);
            var result = "";

            if (index_dot_end != "-1") {
                var data = chuoi.substring(0, index_dot_end);
                result = mysplit(data, style1);

                var form_dot_end = chuoi.substring(index_dot_end, chuoi.length);
                if (typeof (dec) == "undefined") {
                    if (form_dot_end.length == 2)
                        result += chuoi.substring(index_dot_end, chuoi.length) + '0';
                    else if (form_dot_end.length == 1)
                        result += chuoi.substring(index_dot_end, chuoi.length) + '00';
                    else
                        result += chuoi.substring(index_dot_end, index_dot_end + 3);
                }
                else {
                    var i = 0 ;
                    for (i = form_dot_end.length - 1; i < dec; i++) 
                    {
                        form_dot_end = form_dot_end + "0";
                    }
                    result = result + form_dot_end.substr(0, dec + 1);
                }
                var arrResult = result.split(style2);
                while (arrResult[1].length != 0 && arrResult[1].charAt(arrResult[1].length -1) == "0") {
                    if (arrResult[1].charAt(arrResult[1].length - 1) == "0") {
                        arrResult[1] = arrResult[1].substr(0 ,arrResult[1].length -1);
                    }
                }
                if (arrResult[1].length == 0) {
                    result = arrResult[0];
                }
            }
            else {
                result = mysplit(chuoi, style1);
            }
            if (element.val().charAt(0) == "-") {
                element.val("-" + result);
            }
            else {
                element.val(result);
            }
        }
        else {
            //element.val("");
        }
    }
    

    var splittedMoney = false;

    $("." + cssClass).bind("keydown blur", function(e) {
        if (e.type == "keydown") {
            var key = e.which;
            if ((key == 13) && !e.shiftKey) {
                split($(this), style1, style2);
            }
        }
        else if (e.type == "blur") {
        split($(this), style1, style2);
        split($(this), style1, style2);
        }
        
    })

    
    $("." + cssClass).bind("focus", function() {
        var element = $(this);
        var mymoney = element.val();

        var value = mymoney.split(style1);
        var valNew = "";
        for (var i = 0; i < value.length; i++) {
            valNew = valNew + value[i];
        }
        element.val(valNew);
    })
    $("." + cssClass).each(function() {
        var element = $(this);
        if (typeof (align) == "undefined") {
            document.getElementById(element.attr("id")).style.textAlign = "right";
        }
        else {
            document.getElementById(element.attr("id")).style.textAlign = align;
        }
        
        split(element, style1, style2);
        element.addClass("disable_ime-mode");
    })
        
}

function formatNumber(cssClass) {
    function removeZero(val) {
        if (val != "") {
            while (val.length != 0) {
                if (val.charAt(0) == "0") {
                    val = val.substring(1, val.length);
                }
                else {
                    if (val.charAt(0) == ",") {
                        return "0" + val;
                    }
                    return val;
                }
            }
            if (val.length == 0)
                return "0";
            return val;
        }
        return "";
    }
    $("." + cssClass).bind("keydown blur", function(e) {
        if (e.type == "keydown") {
            var key = e.which;
            if ((key == 13) && !e.shiftKey) {
                $(this).val(removeZero($(this).val()));
            }
        }
        else if (e.type == "blur") {
            $(this).val(removeZero($(this).val()));
        }

    })
}

function formatTime(cssClass) {
    $("." + cssClass).mask('99:99');
    $("." + cssClass).bind("paste", function() {
        $(this).val("");
    })
}

function formatZipCode(cssClass) {
    $("." + cssClass).mask('999-9999');
    $("." + cssClass).bind("paste", function() {
        $(this).val("");
    })
}

function disable(validatorId, enable) {
    var validator = document.getElementById(validatorId);
    ValidatorEnable(validator, enable);
}
function ValidatorEnable(val, enable) {
    val.enabled = (enable != false);
    if (!enable) {
        

        if (val.ValidatorCalloutBehavior != null) {

            if (val.ValidatorCalloutBehavior._elementToValidate != null && val.ValidatorCalloutBehavior._highlightCssClass != "") {
                Sys.UI.DomElement.removeCssClass(val.ValidatorCalloutBehavior._elementToValidate, val.ValidatorCalloutBehavior._highlightCssClass)
            }
            val.ValidatorCalloutBehavior._invalid = false;
            val.ValidatorCalloutBehavior.hide(); 
            //ValidatorUpdateDisplay(val);

        }
        ValidatorValidate(val);
        ValidatorUpdateIsValid();
    }
}

function ValidatorEnable2(val, enable) {
    val.enabled = (enable != false);
    if (!enable) {


        if (val.ValidatorCalloutBehavior != null) {

            if (val.ValidatorCalloutBehavior._elementToValidate != null && val.ValidatorCalloutBehavior._highlightCssClass != "") {
                Sys.UI.DomElement.removeCssClass(val.ValidatorCalloutBehavior._elementToValidate, val.ValidatorCalloutBehavior._highlightCssClass)
            }
            val.ValidatorCalloutBehavior._invalid = false;
            val.ValidatorCalloutBehavior.hide();
            //ValidatorUpdateDisplay(val);

        }

    }
    ValidatorValidate(val);
    ValidatorUpdateIsValid();
}

function RemoveInvalid(val) {
    if (val != null) {
        if (val.ValidatorCalloutBehavior != null) {

            if (val.ValidatorCalloutBehavior._elementToValidate != null && val.ValidatorCalloutBehavior._highlightCssClass != "") {
                Sys.UI.DomElement.removeCssClass(val.ValidatorCalloutBehavior._elementToValidate, val.ValidatorCalloutBehavior._highlightCssClass)
            }
            val.ValidatorCalloutBehavior._invalid = false;
            val.ValidatorCalloutBehavior.hide();
        }
    }
}
var list_ddlControls = null;
var list_ddlValue = null;
function callchange(el) {
    $.each(list_ddlControls, function(index, value) {
        if (value == el.id) {
            if (el.value != list_ddlValue[index]) {
            
                setTimeout('__doPostBack(\'' + el.getAttribute('name') + '\',\'\')', 100);
                list_ddlValue[index] = el.value;
            }
        }
    })
    
}
function callchange2(el) {
    $.each(list_ddlControls, function(index, value) {
        if (value == el.id) {
            if (el.value != list_ddlValue[index]) {
                if(el.value != '-1')
                {
                    setTimeout('__doPostBack(\'' + el.getAttribute('name') + '\',\'\')', 100);
                }
                list_ddlValue[index] = el.value;
            }
        }
    })
    
}
function loadDefault_ddlValue() {
    list_ddlValue = new Array();
    $.each(list_ddlControls, function(index, value) {
        list_ddlValue[index] = $("#" + value).val();
    })
}
var var_disableControlOnLoadUpdatePanel = false;
function disableControlOnLoadUpdatePanel(updateId, controlId) {
    if (!var_disableControlOnLoadUpdatePanel) {
        try {

            if (controlId.indexOf(",") != -1) {
                list_ddlControls = controlId.split(",");
                $.each(controlId.split(","), function(index, value) {
                    $("#" + value).attr("onchange", "");
                    document.getElementById(value).setAttribute('onblur', 'callchange(this)');
                    document.getElementById(value).setAttribute('onclick', 'callchange(this)');
                })
                loadDefault_ddlValue();
            }
            else {
                if (document.getElementById(controlId) != null) {
                    list_ddlControls = Array(controlId);
                    $("#" + controlId).attr("onchange", "");
                    document.getElementById(controlId).setAttribute('onblur', 'callchange(this)');
                    document.getElementById(controlId).setAttribute('onclick', 'callchange(this)');
                    loadDefault_ddlValue();
                }
                else {
                    list_ddlControls = Array();
                }
            }

            

            var elementBlock = null;
            if ($.browser.msie) {
                elementBlock = $(".corner");
            }
            else {
                elementBlock = $(".corner").parents("table");
            }
            Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(function() {
                $(".ajax__validatorcallout_popup_table").each(function() {
                    var popup = $(this);
                    var e = document.getElementById(popup.attr('id'));
                    e.PopupBehavior.hide();
                });
                elementBlock.block_signed({ message: '<div style="font-family:MS UI Gothic;background-color:Transparent"><img src="../images/loading.gif" /><br/><div style="width:100%; text-align:center;">データ読取り中、しばらくお待ちください...<div></div>',
                    css: { width: elementBlock.width(),
                        border: 0, background: '#fff', color: '#000', left: '45%'
                    },
                    overlayCSS: {
                        backgroundColor: '#fff',
                        opacity: 1,
                        cursor: 'default'
                    },
                    theme: false
                });
                var_registerControlToValidation = false;
                var_disableControlOnLoadUpdatePanel = false;
            });

            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function() {
                elementBlock.unblock();

            });
            var_disableControlOnLoadUpdatePanel = true;

        }
        catch (err) { }
    }
}
function disableControlOnLoadUpdatePanel2(updateId, controlId) {
    if (!var_disableControlOnLoadUpdatePanel) {
        try {

            if (controlId.indexOf(",") != -1) {
                list_ddlControls = controlId.split(",");
                $.each(controlId.split(","), function(index, value) {
                    $("#" + value).attr("onchange", "");
                    document.getElementById(value).setAttribute('onblur', 'callchange2(this)');
                    document.getElementById(value).setAttribute('onclick', 'callchange2(this)');
                })
                loadDefault_ddlValue();
            }
            else {
                if (document.getElementById(controlId) != null) {
                    list_ddlControls = Array(controlId);
                    $("#" + controlId).attr("onchange", "");
                    document.getElementById(controlId).setAttribute('onblur', 'callchange2(this)');
                    document.getElementById(controlId).setAttribute('onclick', 'callchange2(this)');
                    loadDefault_ddlValue();
                }
                else {
                    list_ddlControls = Array();
                }
            }

            

            var elementBlock = null;
            if ($.browser.msie) {
                elementBlock = $(".corner");
            }
            else {
                elementBlock = $(".corner").parents("table");
            }
            Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(function() {
                $(".ajax__validatorcallout_popup_table").each(function() {
                    var popup = $(this);
                    var e = document.getElementById(popup.attr('id'));
                    e.PopupBehavior.hide();
                });
                elementBlock.block_signed({ message: '<div style="font-family:MS UI Gothic;background-color:Transparent"><img src="../images/loading.gif" /><br/><div style="width:100%; text-align:center;">データ読取り中、しばらくお待ちください...<div></div>',
                    css: { width: elementBlock.width(),
                        border: 0, background: '#fff', color: '#000', left: '45%'
                    },
                    overlayCSS: {
                        backgroundColor: '#fff',
                        opacity: 1,
                        cursor: 'default'
                    },
                    theme: false
                });
                var_registerControlToValidation = false;
                var_disableControlOnLoadUpdatePanel = false;
            });

            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function() {
                elementBlock.unblock();

            });
            var_disableControlOnLoadUpdatePanel = true;

        }
        catch (err) { }
    }
}


//////////////////////////VALIDATION FUNCTION/////////////////////////

var dtCh = "/";
var minYear = 1900;
var maxYear = 2078;

function checkDate(val) {
    if (val.length != 8 && val.length != 10) {
        return false;
    }

    var dtStr = val
    var daysInMonth = DaysArray(12)
    var pos1 = dtStr.indexOf(dtCh)
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1)


    var strYear;
    var strMonth;
    var strDay;
    if (dtStr.length == 10) {
        strYear = dtStr.substring(0, pos1)
        strMonth = dtStr.substring(pos1 + 1, pos2)
        strDay = dtStr.substring(pos2 + 1)
    }
    else if (dtStr.length == 8) {
        strYear = dtStr.substr(0, 4);
        strMonth = dtStr.substr(4, 2);
        strDay = dtStr.substr(6, 2);
    }

    if (strDay.charAt(0) == "0") {
        strDay = strDay.charAt(1);
    }
    if (strMonth.charAt(0) == "0") {
        strMonth = strMonth.charAt(1);
    }
    while (strYear.charAt(0) == "0") {
        strYear = strYear.substring(1);
    }

    month = parseInt(strMonth)
    day = parseInt(strDay)
    year = parseInt(strYear)

    if (strMonth.length < 1 || month < 1 || month > 12) {
        return false;
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        return false;
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
        return false;
    }
    if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
        return false;
    }
    return true;
}

function isDate(oSrc, args) {
    args.IsValid = checkDate(args.Value);
}

function checkRequired(args, val) {
    if (args == "" || args == val || (args.indexOf("_") != -1)) {
        return false;
    }
    return true;
}

function RequiredField_ZipCode(oSrc, args) {
    args.IsValid = checkRequired(args.Value,"___-____");
}

function RequiredField_Time(oSrc, args) {
    args.IsValid = checkRequired(args.Value, "__:__");
}

function checkTime(oSrc, args) {
    if (checkRequired(args.Value, "__:__")) {
        var valTime = args.Value;
        var arrTime = valTime.split(":");
        var hour = arrTime[0];
        var minute = arrTime[1];
        if (hour.charAt(0) == "0") {
            hour = hour.substring(1, 1);
        }
        if (minute.charAt(0) == "0") {
            minute = minute.substring(1, 1);
        }
        var intHour = parseInt(hour)
        if (intHour > 23 || intHour < 0) {
            args.IsValid = false;
            return false;
        }
        var intMinute = parseInt(minute)
        if (intMinute > 59 || intMinute < 0) {
            args.IsValid = false;
            return false;
        }  
    }
}

function checkYearOld(date1, date2, yearOld, op) {
    if (checkDate(date1) && checkDate(date2)) {
        var date1_strYear;
        var date1_strMonth;
        var date1_strDay;
        var date2_strYear;
        var date2_strMonth;
        var date2_strDay;

        var date1_pos1 = date1.indexOf(dtCh)
        var date1_pos2 = date1.indexOf(dtCh, date1_pos1 + 1)
        if (date1.length == 10) {
            date1_strYear = date1.substring(0, date1_pos1)
            date1_strMonth = date1.substring(date1_pos1 + 1, date1_pos2)
            date1_strDay = date1.substring(date1_pos2 + 1)
        }
        else if (date1.length == 8) {
            date1_strYear = date1.substr(0, 4);
            date1_strMonth = date1.substr(4, 2);
            date1_strDay = date1.substr(6, 2);
        }
        var date2_pos1 = date2.indexOf(dtCh)
        var date2_pos2 = date2.indexOf(dtCh, date2_pos1 + 1)
        if (date2.length == 10) {
            date2_strYear = date2.substring(0, date2_pos1)
            date2_strMonth = date2.substring(date2_pos1 + 1, date2_pos2)
            date2_strDay = date2.substring(date2_pos2 + 1)
        }
        else if (date2.length == 8) {
            date2_strYear = date2.substr(0, 4);
            date2_strMonth = date2.substr(4, 2);
            date2_strDay = date2.substr(6, 2);
        }

        if (date2_strYear - date1_strYear == yearOld) {
            if (date1_strMonth < date2_strMonth) {
                return true;
            }
            else if (date1_strMonth > date2_strMonth) {
                return false;
            }
            else {
                if (op == "<") {
                    if (date2_strDay <= date1_strDay) {
                        return false;
                    }
                }
                else if (op == "<=") {
                    if (date2_strDay < date1_strDay) {
                        return false;
                    }
                }
            }

        }
        else if (date2_strYear - date1_strYear < yearOld) {
            return false;
        }
        return true;
    }
    return true;
}

function checknumber(n, style1) {
    if (n.length != 0) {
        if (n.indexOf(style1, 0) != -1) {
            var value = n.split(style1);
            var valNew = "";
            for (var i = 0; i < value.length; i++) {
                valNew = valNew + value[i];
            }
            n = "-" + valNew;
        }
        return !isNaN(parseFloat(n)) && isFinite(n);
    }
    //return "";
}

function checkMoney(oSrc, args) {
    if (args.Value != "") {
        var chuoi;
        if (args.Value.charAt(0) == "-" && args.Value.indexOf(",") != -1) {
            chuoi = args.Value.substring(1, args.Value.length);
            
        }
        else {
            chuoi = args.Value;

        }
        chuoi = removeZero(chuoi);
        args.IsValid = checknumber(chuoi, ",");
    }
    else {
        args.IsValid = true;
    }
    function removeZero(val) {
        if (val != "") {
            while (val.length != 0) {
                if (val.charAt(0) == "0") {
                    val = val.substring(1, val.length);
                }
                else {
                    if (val.charAt(0) == ",") {
                        return "0" + val;
                    }
                    return val;
                }
            }
            if (val.length == 0)
                return "0";
            return val;
        }
        return val;
    }
}
function checkMoney2(oSrc, args) {
    if (args.Value != "") {
        var chuoi = removeZero(args.Value);
        if (chuoi.charAt(0) == "-") {
            chuoi = "-" + chuoi;
        }
        if (chuoi.charAt(0) == "-" && chuoi.indexOf(",") != -1) {
            chuoi = chuoi.substring(1, chuoi.length);

        }
        chuoi = removeZero(chuoi);
        args.IsValid = checknumber(chuoi, ",");
    }
    else {
        args.IsValid = true;
    }
    function removeZero(val) {
        if (val != "") {
            while (val.length != 0) {
                if (val.charAt(0) == "0") {
                    val = val.substring(1, val.length);
                }
                else {
                    if (val.charAt(0) == ",") {
                        return "0" + val;
                    }
                    return val;
                }
            }
            if (val.length == 0)
                return "0";
            return val;
        }
        return val;
    }
}

function getNowDate(_nowdate) {
    var Date = _nowdate.getDate();
    if (Date < 10) {
        Date = "0" + Date.toString();
    }
    var Month = _nowdate.getMonth() + 1;
    if (Month < 10) {
        Month = "0" + Month.toString();
    }
    return _nowdate.getFullYear().toString() + "/" + Month + "/" + Date; 
}


function daysInFebruary(year) {
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
        if (i == 2) { this[i] = 29 }
    }
    return this
}
function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function testPhone(txt1, txt2, txt3, argsVal) {
    var val1 = txt1.value;
    var val2 = txt2.value;
    var val3 = txt3.value;
    if (val1 != "" || val2 != "" || val3 != "") {
        if (argsVal == '') {
            return false;
        }
        else {
            return true;
        }
    }
    else {
        $("#" + txt1.id).removeClass("invalid invalid1 invalid2 invalid3 invalid4 invalid5");
        $("#" + txt2.id).removeClass("invalid invalid1 invalid2 invalid3 invalid4 invalid5");
        $("#" + txt3.id).removeClass("invalid invalid1 invalid2 invalid3 invalid4 invalid5");
        return true;
    }

}

function check2Byte(oSrc, args) {
    var val = args.Value;
    for (var i = 0; i < val.length; i++) {
        if (escape(val.charAt(i)).length < 4) {
            args.IsValid = false;
            return false;
        } 
        
    }
}

function CheckNotInputSpecialKeys(oSrc, args) {
    if (args.Value != "") {
        for (var i = 0; i < args.Value.length; i++) {
            if ((("`~!@#$%^&*()-_+=[]{};:'\"\\|.,<>/? ").indexOf(args.Value.charAt(i)) > -1)) {
                args.IsValid = false;
                break;
            }
        }
    }
}

function CheckInputNumberKeys(oSrc, args) {
    if (args.Value != "") {
        for (var i = 0; i < args.Value.length; i++) {
            if ((("0123456789").indexOf(args.Value.charAt(i)) == -1)) {
                args.IsValid = false;
                break;
            }
        }
    }
}
(function() {
        String.prototype.to_utf8_byte_array = function() {
            var bytes = [];

            var s = unescape(encodeURIComponent(this));

            for (var i = 0; i < s.length; i++) {
                var c = s.charCodeAt(i);
                bytes.push(c);
            }

            return bytes;
        };

        /*
        * Returns the number of bytes in the given UTF-8 string.
        */
        String.prototype.utf8_num_bytes = function() {
            return unescape(encodeURIComponent(this)).length;
        };

    })();
function check1Byte(oSrc, args) {
    var val = args.Value;
    for (var i = 0; i < val.length; i++) {
        if (val.charAt(i).utf8_num_bytes() > 1) {
            args.IsValid = false;
            return false;
        }

    }

}
function checkHiraAndRomaji(oSrc, args) {
    args.IsValid = isnKatakana(args.Value);
}
function checkKatakana(oSrc, args) {
    args.IsValid = isKatakana(args.Value);
}

